﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }

        public EditForm(int Id, string Title, string Author, string CreatedTime)
        {
            InitializeComponent();
            this.Id = Id;
            textBox1.Text = Title;
            textBox2.Text = Author;
            textBox3.Text = CreatedTime;
        }
        private int Id;


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var Title = textBox1.Text;
            var Author = textBox2.Text;
            var CreatedTime = textBox3.Text;


            //更新
            if (Id > 0)
            {
                if (!string.IsNullOrEmpty(Title))
                {
                    var sql = string.Format("update Articles set Tltle='{0}',Author='{1}',CreatedTime={2} where Id={3}", Title, Author, CreatedTime, this.Id);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("修改成功！！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }

                }
            }
            //新增
            else
            {
                if (!string.IsNullOrEmpty(Title))
                {
                    var sql = string.Format("insert into Articles (Title,Author,CreatedTime) values ('{0}','{1}',{2}) ", Title, Author, CreatedTime);

                    var res = DbHelper.AddOrEditOrDeleteSave(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败！", "提示");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("学生姓名不能为空！", "提示");
                }
            }
        }
        
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
